﻿using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.CreateAvatarName
{
    public class AvatarNameMenu : BaseMenuScreen<MainMenu.Menu>
    {
        public string CurrentName { get; private set; }
        private Label _labelName;

        private Keyboard _keyboard;

        public AvatarNameMenu(TE_Vector2 size, string currentName)
        {
            TE_Vector2 keyboardAreaSize = new TE_Vector2((int) (size.X - 1), (int)(size.Y * 0.65f));
            TE_Vector2 keyboardPosition = new TE_Vector2((size.X - keyboardAreaSize.X) / 2, size.Y - keyboardAreaSize.Y - 0.5f);

            _keyboard = new Keyboard(keyboardAreaSize) {
                                                        PositionGridBound = keyboardPosition
                                                    };

            CurrentName = currentName;
            _labelName = new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, CurrentName.Length > 0 ? CurrentName : "_") {
                                                                                                              PositionGridBound = new TE_Vector2(keyboardPosition.X, keyboardPosition.Y - 2),
                                                                                                              TextAlign = TE_Rectangle.Alignment.CenterLeft,
                                                                                                              Scale = new TE_Vector2(5, 4),
                                                                                                          };

            var title = Title.Blue("Enter a character name", size);
            AddChildren(_keyboard, _labelName, title);
        }

        public override void UpKey(int id)
        {
            _keyboard.MoveUp();
        }

        public override void DownKey(int id)
        {
            _keyboard.MoveDown();
        }

        public override void LeftKey(int id)
        {
            _keyboard.MoveLeft();
        }

        public override void RightKey(int id)
        {
            _keyboard.MoveRight();
        }

        public override void AcceptKey(int id)
        {
            var character = _keyboard.CurrentKey;

            if(character == "Enter")
            {
                if(CurrentName.Length > 0)
                    ChangeScreen(-1, MainMenu.Menu.CreateNewAvatar_Color);
                //Change menu
                return;
            }
            if(character == "<")
            {
                if(CurrentName.Length > 0)
                    CurrentName = CurrentName.Substring(0, CurrentName.Length - 1);
            }
            else
            {
                if(character == "_")
                    character = " ";

                if(CurrentName.Length < 10)
                    CurrentName += character;
            }

            _labelName.SetText(CurrentName.Length == 0 ? "_" : CurrentName);
        }

        public override void CancelKey(int id)
        {
            ChangeScreen(-1, MainMenu.Menu.SelectAvatar);
        }
    }
}